package com.dp.service.impl3;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import com.dp.util.BaseBean;
import com.dp.util.DataMap;
import com.dp.util.InterUtil;
import com.dp.util.KeyGenerator;
import com.dp.util.PageBean;

@Service
@Transactional
public class UserAgentBeanImpl extends BaseImpl {
	private static Logger log = Logger.getLogger(UserAgentBeanImpl.class);
	
//	public static PageBean getUserAgent(DataMap hashMap) {
//
//		String sql = "SELECT * FROM t_useragents as t1  where 1=1" ;
//
////		" where ((t1.employee_id= "
////				+ ObjectToStringDB(hashMap.get("employee_id"))+") or (t1.status in('new','unpass','pass','publish')))";
////		
//
//		if(StringIsNull(hashMap.get("employee_id")))
//		{
//			sql=sql+" and employee_id="+ObjectToStringDB(hashMap.get("employee_id"));
//		}
//		
//		
//		if(StringIsNull(hashMap.get("useragent")))
//		{
//			sql=sql+" and useragent="+ObjectToStringDB(hashMap.get("useragent"));
//		}
//		log.debug("sql="+sql);
//		try {
//			hashMap.put("sql", sql);
//
//			return new PageBean(hashMap);
//		} catch (Exception e) {
//			return null;
//		}
//	}
//	public static PageBean getAuditUserAgentList(DataMap hashMap) {
//
//		String sql = "SELECT * FROM t_useragents as t1 " +
//
//		" where t1.status ='auditing' ";
//		
//		if(StringIsNull(hashMap.get("useragent")))
//		{
//			sql=sql+" and useragent="+ObjectToStringDB(hashMap.get("useragent"));
//		}
//		
//		try {
//			hashMap.put("sql", sql);
//
//			return new PageBean(hashMap);
//		} catch (Exception e) {
//			return null;
//		}
//	}

//	public  List getUserAgent() {
//
//		String querySql = "SELECT * FROM t_useragents as t1 " +
//
//		" where t1.status= 'publish'";
//		
//
//		try {
//			
//
//			return this.queryALLResultSet(querySql);
//		} catch (Exception e) {
//			return null;
//		}
//	}
//	public List getUseUserAgent(String uagroupid) {
//
//		String querySql = "SELECT * FROM t_useragents as t1 , t_useragent_uagroup as t2 " +
//
//		" where t1.useragentid=t2.useragentid and  t2.uagroupid= '"+uagroupid+"'";
//		
//
//		try {
//			
//
//			return this.queryALLResultSet(querySql);
//		} catch (Exception e) {
//			return null;
//		}
//	}
//	public DataMap getUserAgent(String useragentid) {
//
//		String querySql = "SELECT * FROM t_useragents as t1 " +
//
//		" where t1.useragentid= '"+useragentid+"'";
//		
//
//		try {
//			
//			return null;
//			//return this.queryOneRow(querySql);
//		} catch (Exception e) {
//			return null;
//		}
//	}
	public boolean addUserAgent(DataMap hashMap) {

		String userAgentid=KeyGenerator.getInstance().getNextKeyValue("t_useragents");
		String querySql = "insert into t_useragents(useragentid,employee_id,useragent,description,stream,manufacturer,model,encoding,mediatype," +
				"devicekey,screenwidth,screenheight,memorysize,installguide,supportedcontenttypes,status) " +
		" values(" +
		ObjectToStringDB(userAgentid)+","+
		ObjectToStringDB(hashMap.get("employee_id"))+","+
		ObjectToStringDB(hashMap.get("useragent"))+","+
		ObjectToStringDB(hashMap.get("description"))+","+
		ObjectToStringDB(hashMap.get("stream"))+","+
		ObjectToStringDB(hashMap.get("manufacturer"))+","+
		ObjectToStringDB(hashMap.get("model"))+","+
		ObjectToStringDB(hashMap.get("encoding"))+","+
		ObjectToStringDB(hashMap.get("mediatype"))+","+
		ObjectToStringDB(hashMap.get("devicekey"))+","+
		(hashMap.get("screenwidth"))+","+
		(hashMap.get("screenheight"))+","+
		(hashMap.get("memorysize"))+","+
		ObjectToStringDB(hashMap.get("installguide"))+","+
		
		ObjectToStringDB(hashMap.get("supportedcontenttypes"))+","+ 
		"'new')";
		
		log.debug("querySql="+querySql);
		try {
			

			return this.execUpdate(querySql);
		} catch (Exception e) {
			return false;
		}
	}
	
	
	
	
	
//	public boolean delUserAgent(DataMap hashMap) {
//
//		
//		String querySql = "select DISTINCT t1.* from t_employee as t1 ,t_uagroups as t2 ,t_useragent_uagroup as t3 "+
//						 "where t1.employee_id=t2.employee_id " +
//						 "and t2.uagroupid=t3.uagroupid " +
//						 "and t3.useragentid='"+hashMap.get("useragentid")+"'";
//		
//		try {
//			log.debug("querySql="+querySql);
//			List list=this.queryALLResultSet(querySql);
//			if(list.size()>0)
//			{
//				String info=InterUtil.rb.getString("src.userIs")+"：<br>";
//				DataMap map=null;
//				for(int i=0;i<list.size();i++)
//				{
//					map=(DataMap)list.get(i);
//					info=info+map.getMapData("employee_name")+"&nbsp&nbsptel:"+map.getMapData("employee_tel")+"&nbsp&nbspphone:"+map.getMapData("employee_phone")+"<br>";
//				}
//				hashMap.put("beanInfo", info);
//				return false;
//			}
//			querySql="delete from t_useragents where useragentid='"+hashMap.get("useragentid")+"'";
//			log.debug("querySql="+querySql);
//			return this.execUpdate(querySql);
//		} catch (Exception e) {
//			return false;
//		}
//	}
	public boolean updateUserAgent(DataMap hashMap) {

		
		String querySql = "update   t_useragents set "+
		"useragent="+ObjectToStringDB(hashMap.get("useragent"))+", "+
		"description="+ObjectToStringDB(hashMap.get("description"))+", "+
		"stream="+ObjectToStringDB(hashMap.get("stream"))+", "+
		"manufacturer="+ObjectToStringDB(hashMap.get("manufacturer"))+", "+
		"model="+ObjectToStringDB(hashMap.get("model"))+", "+
		"encoding="+ObjectToStringDB(hashMap.get("encoding"))+", "+
		"mediatype="+ObjectToStringDB(hashMap.get("mediatype"))+", "+
		"devicekey="+ObjectToStringDB(hashMap.get("devicekey"))+", "+
		"screenwidth="+hashMap.get("screenwidth")+", "+
		"screenheight="+hashMap.get("screenheight")+", "+
		"memorysize="+hashMap.get("memorysize")+", "+
		"installguide="+ObjectToStringDB(hashMap.get("installguide"))+","+
		
		"supportedcontenttypes="+ObjectToStringDB(hashMap.get("supportedcontenttypes"))+" "+
		" where useragentid="+ObjectToStringDB(hashMap.get("useragentid")); 
		
		
		log.debug("querySql="+querySql);
		try {
			


			return this.execUpdate(querySql);
		} catch (Exception e) {
			return false;
		}
	}
	public boolean auditUserAgent(DataMap hashMap,String status) {

		log.debug(StringIsNull(hashMap.get("issue")));
		StringTokenizer st=new StringTokenizer(hashMap.getMapData("gvalue"),"|");
		String querySql = "update   t_useragents set status='"+status+"' ";
		
		if(StringIsNull(hashMap.get("issue")))
		{
			querySql=querySql+" , issue="+ObjectToStringDB(hashMap.get("issue"));
		}
		querySql=querySql+" where useragentid in (";
		while(st.hasMoreElements())
		{
			querySql=querySql+ObjectToStringDB(st.nextElement())+",";
		}
		querySql=querySql+"'testString')";
		
		
		log.debug("querySql="+querySql);
		try {
			


			return this.execUpdate(querySql);
		} catch (Exception e) {
			return false;
		}
	}
	
	public boolean addMyList(DataMap hashMap) {

		String uagroupid=KeyGenerator.getInstance().getNextKeyValue("t_uagroups");
		String querySql = "insert into t_uagroups (uagroupid,employee_id,groupname,info)" +
						   " values (" +
		ObjectToStringDB(uagroupid)+","+
		ObjectToStringDB(hashMap.get("employee_id"))+","+
		ObjectToStringDB(hashMap.get("groupname"))+","+
		ObjectToStringDB(hashMap.get("info"))+")";
		
		log.debug("querySql="+querySql);
		
		ArrayList list=new ArrayList();
		list.add(querySql);
		
		StringTokenizer st=new StringTokenizer(hashMap.getMapData("gvalue"),"|");
		
		while(st.hasMoreElements())
		{
			querySql= "insert into t_useragent_uagroup (uagroupid,useragentid)" +
			   " values (" ;
			querySql=querySql+ObjectToStringDB(uagroupid)+","+ObjectToStringDB(st.nextElement())+")";
			list.add(querySql);
		}
		try {
			
			log.debug("list "+list);
			
			return this.execTransaction(list);
		} catch (Exception e) {
			return false;
		}
	}
	
	
	public boolean delUaGroups(DataMap hashMap) {

		
		String[] querySql={"delete from t_uagroups where uagroupid='"+hashMap.get("uagroupid")+"'"
						  ,"delete from t_useragent_uagroup where uagroupid='"+hashMap.get("uagroupid")+"'"};
		
		try {
			
			log.debug("querySql="+querySql);
			return this.execTransaction(querySql);
		} catch (Exception e) {
			return false;
		}
	}
	
	public boolean modifmylist(DataMap hashMap) {

		ArrayList list=new ArrayList();
		
		
		String querySql="delete from t_uagroups where uagroupid='"+hashMap.get("uagroupid")+"'";
		list.add(querySql);
		
		querySql="delete from t_useragent_uagroup where uagroupid='"+hashMap.get("uagroupid")+"'";
		list.add(querySql);
		
		String uagroupid=KeyGenerator.getInstance().getNextKeyValue("t_uagroups");
		querySql = "insert into t_uagroups (uagroupid,employee_id,groupname,info)" +
						   " values (" +
		ObjectToStringDB(uagroupid)+","+
		ObjectToStringDB(hashMap.get("employee_id"))+","+
		ObjectToStringDB(hashMap.get("groupname"))+","+
		ObjectToStringDB(hashMap.get("info"))+")";
		
		list.add(querySql);
		
		
		StringTokenizer st=new StringTokenizer(hashMap.getMapData("gvalue"),"|");
		
		while(st.hasMoreElements())
		{
			querySql= "insert into t_useragent_uagroup (uagroupid,useragentid)" +
			   " values (" ;
			querySql=querySql+ObjectToStringDB(uagroupid)+","+ObjectToStringDB(st.nextElement())+")";
			list.add(querySql);
		}
		try {
			
			log.debug("querySql="+list);
			return this.execTransaction(list);
		} catch (Exception e) {
			return false;
		}
	}
	
	
	
//	public PageBean getmobileCompanyList(DataMap hashMap) {
//
//		String sql = "SELECT * FROM t_mobile_company_info " ;
//
////		" where ((t1.employee_id= "
////				+ ObjectToStringDB(hashMap.get("employee_id"))+") or (t1.status in('new','unpass','pass','publish')))";
////		
//
//		if(StringIsNull(hashMap.get("COM_ID")))
//		{
//			
//			sql=sql+" where COM_ID="+ObjectToStringDB(hashMap.get("id"));
//		}
//		if(StringIsNull(hashMap.get("AREA")))
//		{
//			sql=sql+" where AREA="+ObjectToStringDB(hashMap.get("AREA"));
//		}
//		
//		if(StringIsNull(hashMap.get("STATE")))
//		{
//			sql=sql+" and STATE="+ObjectToStringDB(hashMap.get("STATE"));
//		}
//		
//		if(StringIsNull(hashMap.get("COM_NAME")))
//		{
//			sql=sql+" and COM_NAME="+ObjectToStringDB(hashMap.get("COM_NAME"));
//		}
//		log.debug("sql="+sql);
//		try {
//			hashMap.put("sql", sql);
//
//			return new PageBean(hashMap);
//		} catch (Exception e) {
//			return null;
//		}
//	}
	
	

	
	/**
	 * add mobile company
	 * @param hashMap
	 * @return
	 */
	public boolean addCompany(DataMap hashMap) {

//		String userAgentid=KeyGenerator.getInstance().getNextKeyValue("t_useragents");
		String querySql = "insert into t_mobile_company_info(com_id, com_en_name,com_name,com_ua_character,area,ranking,state,upd_time,ua_first_character)"
		+"values(null, " +
		ObjectToStringDB(hashMap.get("COM_EN_NAME"))+","+
		ObjectToStringDB(hashMap.get("COM_NAME"))+","+
		ObjectToStringDB(hashMap.get("COM_UA_CHARACTER"))+","+
		ObjectToStringDB(hashMap.get("AREA"))+","+
		ObjectToStringDB(hashMap.get("RANKING"))+", '0',now(),"+
		ObjectToStringDB(hashMap.get("UA_FIRST_CHARACTER"))+")";
		
		log.debug("querySql="+querySql);
		try {
		
			return this.execUpdate(querySql);
		} catch (Exception e) {
			return false;
		}
	}
	/**
	 * update mobile company
	 * @param hashMap
	 * @return
	 */
	public boolean updateCompany(DataMap hashMap) {

//		String userAgentid=KeyGenerator.getInstance().getNextKeyValue("t_useragents");
		String querySql = "update t_mobile_company_info set "
			+"COM_EN_NAME = "+ObjectToStringDB(hashMap.get("com_en_name"))
			+",COM_NAME = "+ObjectToStringDB(hashMap.get("com_name"))
			+",COM_UA_CHARACTER = "+ObjectToStringDB(hashMap.get("com_ua_character"))
			+", AREA = "+ObjectToStringDB(hashMap.get("area"))
			+", RANKING = "+ObjectToStringDB(hashMap.get("ranking"))
			+", UA_FIRST_CHARACTER = "+ObjectToStringDB(hashMap.get("ua_first_character"))
			+" WHERE COM_ID="+ObjectToStringDB(hashMap.get("com_id"));
			
//			com_en_name,com_name,com_ua_character,area,ranking,state,upd_time,ua_first_character)"
//		+"values(null, " +
//		ObjectToStringDB(hashMap.get("COM_EN_NAME"))+","+
//		ObjectToStringDB(hashMap.get("COM_NAME"))+","+
//		ObjectToStringDB(hashMap.get("COM_UA_CHARACTER"))+","+
//		ObjectToStringDB(hashMap.get("AREA"))+","+
//		ObjectToStringDB(hashMap.get("RANKING"))+", '0',now(),"+
//		ObjectToStringDB(hashMap.get("UA_FIRST_CHARACTER"))+")";
		
		log.debug("querySql="+querySql);
		try {
		
			return this.execUpdate(querySql);
		} catch (Exception e) {
			return false;
		}
	}
	
}
